﻿using System;
using System.Globalization;
using System.Windows.Data;
using Models;

namespace SuperQuery.Converters
{
    public class QueryTypeToStringConverter : IValueConverter
    {
        /// <summary>
        /// Converts a value from QueryType to String
        /// </summary>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        /// <param name="value">The value produced by the binding source.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // Default to the canCloseApplication being set to "Auto"
            string result = AUTO;

            if ((value != null) && (value is Query.QueryType))
            {
                Query.QueryType queryType = (Query.QueryType) value;

                switch (queryType)
                {
                    case Query.QueryType.Table:
                        result = TABLE;
                        break;
                    case Query.QueryType.Scalar:
                        result = SCALAR;
                        break;
                    default:
                        result = AUTO;
                        break;
                }
            }

            return result;
        }

        /// <summary>
        /// Converts a value from string to QueryType
        /// </summary>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        /// <param name="value">The value that is produced by the binding target.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {

            // Default to the canCloseApplication being set to "Auto"
            Query.QueryType result = Query.QueryType.Auto;

            if ((value != null) && (value is string))
            {
                string queryTypeString = value.ToString();

                switch (queryTypeString)
                {
                    case TABLE:
                        result = Query.QueryType.Table;
                        break;

                    case SCALAR:
                        result = Query.QueryType.Scalar;
                        break;

                    default:
                        result = Query.QueryType.Auto;
                        break;
                }
            }

            return result;
        }

        private const string AUTO = "Auto";
        private const string TABLE = "Table";
        private const string SCALAR = "Scalar";
    }
}
